﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Web;

[assembly: InternalsVisibleTo("LynxWiki.Tests")]

namespace LynxWiki.ViewModels
{
    public class TopicVersionViewData
    {

        public TopicVersionViewData()
        { }

        public TopicVersionViewData(DomainModel.TopicVersion tv)
        {
            this.Topic = tv;
        }

        private DomainModel.TopicVersion topic;
        public DomainModel.TopicVersion Topic
        {
            get
            {
                if (topic != null) {
                    topic.MinorUpdates = this.CreateListFromMinorUpdateDetails();
                    return topic;
                }
                else
                    return new DomainModel.TopicVersion();
            }
            set
            {
                topic = value;
                TopicTagsToViewTags(topic.Tags);
                MinorUpdateToViewDetail(topic.MinorUpdates);
                AllowEditListUserToView(topic.AllowUpdatePrivsUser);
                DenyReadListUserToView(topic.DenyReadPrivsUser);
                AllowEditListRoleToView(topic.AllowUpdatePrivsRole);
                DenyReadListRoleToView(topic.DenyReadPrivsRole);
            }
        }
        public string TopicName { get; set; }
        public string PagePath { get; set; }
        public string Tags { get; set; }
        public string MinorUpdateDetails { get; set; }
        public string MinorUpdate { get; set; }
        public string ListAllowEditUser { get; set; }
        public string ListDenyReadUser { get; set; }
        public string ListAllowEditRole { get; set; }
        public string ListDenyReadRole { get; set; }
        public bool AllowUpdate { get; set; }
        public bool TopicLock { get; set; }

        public HttpRequestBase FormPostRequest { get; set; }

        [Display(Name = "How much is")]
        public string CaptchaInput { get; set; }

        private void TopicTagsToViewTags(List<string> topicTags)
        {
            string tempTags = string.Empty;
            if (topicTags != null) {
                foreach (string t in topicTags) {
                    if (!string.IsNullOrEmpty(tempTags)) tempTags += ", ";
                    tempTags += t;
                }
            }
            Tags = tempTags;
        }

        public List<DomainModel.MinorUpdateInstance> CreateListFromMinorUpdateDetails()
        {
            List<DomainModel.MinorUpdateInstance> rtnList = new List<DomainModel.MinorUpdateInstance>();
            if (!string.IsNullOrEmpty(MinorUpdateDetails)) {
                ArrayList tempDtl = new ArrayList();
                if (MinorUpdateDetails.Contains(";")) {
                    MinorUpdateDetails = MinorUpdateDetails.Replace(Environment.NewLine, "");
                    tempDtl = new ArrayList(MinorUpdateDetails.Split(';'));
                }
                else {
                    tempDtl.Add(MinorUpdateDetails);
                }
                foreach (string item in tempDtl) {
                    DomainModel.MinorUpdateInstance tempMinor = new DomainModel.MinorUpdateInstance();
                    int dash = item.IndexOf(" - ");
                    int colon = item.IndexOf(": ");
                    string updater = item.Substring(0, dash).Trim();
                    string tempDate = item.Substring(dash + 3, colon - (dash + 3)).Trim();
                    DateTime updatedOn;
                    DateTime.TryParse(tempDate, out updatedOn);
                    string updateRsn = item.Substring(colon + 2, item.Length - (colon + 2)).Trim();
                    tempMinor.UpdatedOn = updatedOn;
                    tempMinor.Updater = updater;
                    tempMinor.UpdateReason = updateRsn;
                    rtnList.Add(tempMinor);
                }
            }
            return rtnList;
        }

        internal void MinorUpdateToViewDetail(List<DomainModel.MinorUpdateInstance> minor)
        {
            string tempMinor = string.Empty;
            if (minor != null) {
                foreach (DomainModel.MinorUpdateInstance mui in minor) {
                    if (!string.IsNullOrEmpty(tempMinor)) tempMinor += ";" + Environment.NewLine;
                    tempMinor += mui.Updater + " - " + mui.UpdatedOn + ": " + mui.UpdateReason;
                }
            }
            MinorUpdateDetails = tempMinor;
        }

        private void AllowEditListUserToView(List<string> allowEdit)
        {
            string tempAllow = string.Empty;
            if (allowEdit != null) {
                foreach (string a in allowEdit) {
                    if (!string.IsNullOrEmpty(tempAllow)) tempAllow += ", ";
                    tempAllow += a;
                }
            }
            ListAllowEditUser = tempAllow;
        }

        private void DenyReadListUserToView(List<string> denyRead)
        {
            string tempDeny = string.Empty;
            if (denyRead != null) {
                foreach (string d in denyRead) {
                    if (!string.IsNullOrEmpty(tempDeny)) tempDeny += ", ";
                    tempDeny += d;
                }
            }
            ListDenyReadUser = tempDeny;
        }
        private void AllowEditListRoleToView(List<string> allowEdit)
        {
            string tempAllow = string.Empty;
            if (allowEdit != null) {
                foreach (string a in allowEdit) {
                    if (!string.IsNullOrEmpty(tempAllow)) tempAllow += ", ";
                    tempAllow += a;
                }
            }
            ListAllowEditRole = tempAllow;
        }

        private void DenyReadListRoleToView(List<string> denyRead)
        {
            string tempDeny = string.Empty;
            if (denyRead != null) {
                foreach (string d in denyRead) {
                    if (!string.IsNullOrEmpty(tempDeny)) tempDeny += ", ";
                    tempDeny += d;
                }
            }
            ListDenyReadRole = tempDeny;
        }
    }
}